<?php

/**
 * @author blog.anchen8.net
 * @copyright 2015
 */
class channels extends actionAbstract{
    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
        $this->partnerInfo = $this->userinfo=acl::checkLogin('PmsCookie','/index.php/partner/common/login');
        if($this->partnerInfo['parentid']!=0){
        	$this->partnerInfo['id'] = $this->partnerInfo['parentid'];
        }
    }
    function hotelziduan($ziduan){
        $this->loadModel('hotel','category');
        $re=$this->hotel->categoryModel->fetchRow("select $ziduan from hotel_category where id=".(isset($_SESSION['hotel_id'])?$_SESSION['hotel_id']:0)." and partnerid=".$this->partnerInfo['id']);
    	if($re){
        	return $re[$ziduan];
        }else{
        	return 0;
        }
    }
    
    function index(){
    	$this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("channels/index");
    	if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        $leftpost="channels";
        $leftbot="index";
        $hotelId=$this->hotelziduan('id');
        
        $this->loadHelper('pager');
        $this->loadModel('pms','channels');
        $this->loadModel('pms','chstatus');
        
		$keyName=isset($_GET['keyName'])?($_GET['keyName']):'';
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        $size = 10;
        $offset = ($page - 1) * $size;
        
		$where="id>0";
		if($keyName){
			$where.=" and title like '%".$keyName."%'";
		}
		
        $chR=$this->pms->chstatusModel->fetchAll("select * from pms_chstatus where hotelId=$hotelId");
        $reto=$this->pms->channelsModel->fetchAll("select * from pms_channels where ".$where." order by sort asc");
        
        $count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
        
        //数组分页
        $re=array_slice($reto,$offset,$size);
        
        include $this->loadWidget('partnerTheme');
    }
    
    //渠道开关
    function status(){
    	$str_return = $this->my_authority("channels/status");
    	if(!$str_return){
    		exit(json_encode(array('state'=>0,'html'=>'你没有该权限！')));
    	}
        $hotelId=$this->hotelziduan('id');
        $this->loadModel('pms','chstatus');
        $id=isset($_POST['id'])?(int)($_POST['id']):0;
        $status=isset($_POST['status'])?(int)($_POST['status']):0;
        $chId=isset($_POST['chId'])?(int)($_POST['chId']):0;
        
        if($id>0){
            $data['status']=$status;
            $re=$this->pms->chstatusModel->update($data,"id=$id and hotelId=$hotelId and chId=$chId");
        }else{
            $arr['status']=$status;
            $arr['chId']=$chId;
            $arr['hotelId']=$hotelId;
            $re=$this->pms->chstatusModel->insert($arr);
        }
        
        if($re){
            exit(json_encode(array('state'=>1,'html'=>'操作成功！')));
        }else{
            exit(json_encode(array('state'=>0,'html'=>'操作失败！')));
        }
    }
    
    
}